package controller

import (
	"hello/args"
	"hello/service"
	"hello/util"
	"net/http"
)

/**
  加载群列表
*/
func LoadCommunitys(writer http.ResponseWriter, request *http.Request)  {
	var communityArg args.CommunityArg
	err := util.Bind(*request, &communityArg)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	s := service.CommunityService{
		Args: communityArg,
	}
	list, err := s.List()
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	util.RespOk(writer, list, "")
	return
}
/**
创建群
 */
func CreateCommunity(writer http.ResponseWriter, request *http.Request)  {
	var communityArg args.CommunityArg
	err := util.Bind(*request, &communityArg)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	s := service.CommunityService{
		Args: communityArg,
	}

	err = s.Create()

	if err != nil{
		util.RespFail(writer, err.Error())
		return
	}

	util.RespOk(writer, nil, "添加成功")
	return
}
/**
 加入群
 */
func JoinCommunity(writer http.ResponseWriter, request *http.Request)  {
	var communityArg args.CommunityArg
	err := util.Bind(*request, &communityArg)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	s := service.CommunityService{
		Args: communityArg,
	}

	err = s.Join()

	if err != nil{
		util.RespFail(writer, err.Error())
		return
	}

	util.RespOk(writer, nil, "加入成功")
	return
}
